package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgDrawPrizeRecordMapper;
import com.ygqh.baby.model.DrawReport;
import com.ygqh.baby.model.PrizeItem;
import com.ygqh.baby.model.PrizeItem.Type;
import com.ygqh.baby.po.YgDrawPrizeRecord;
import com.ygqh.baby.po.YgDrawPrizeRecordExample;
import com.ygqh.baby.po.YgDrawPrizeRecordExample.Criteria;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.po.YgUserPrize;
import com.ygqh.baby.service.YgDrawPrizeRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.*;

@Service
public class YgDrawPrizeRecordServiceImpl implements YgDrawPrizeRecordService {

    @Autowired
    private YgDrawPrizeRecordMapper ygDrawPrizeRecordMapper;

    @Override
    public List<YgDrawPrizeRecord> find() {
        YgDrawPrizeRecordExample example = new YgDrawPrizeRecordExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygDrawPrizeRecordMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgDrawPrizeRecordExample example = new YgDrawPrizeRecordExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygDrawPrizeRecordMapper.countByExample(example);
    }

    @Override
    public YgDrawPrizeRecord findById(Long id) {
        return ygDrawPrizeRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(YgDrawPrizeRecord ygDrawPrizeRecord) {
        return ygDrawPrizeRecordMapper.insertSelective(ygDrawPrizeRecord);
    }

    @Override
    public int update(YgDrawPrizeRecord ygDrawPrizeRecord) {
        return ygDrawPrizeRecordMapper.updateByPrimaryKeySelective(ygDrawPrizeRecord);
    }

    @Override
    public ResultSet<YgDrawPrizeRecord> search(QueryInfo queryInfo, String q) {
        List<YgDrawPrizeRecord> list = ygDrawPrizeRecordMapper.selectSuper(queryInfo, q);
        int count = ygDrawPrizeRecordMapper.countSuper(q);
        ResultSet<YgDrawPrizeRecord> resultSet = new ResultSet<YgDrawPrizeRecord>(count, list);
        return resultSet;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        YgDrawPrizeRecordExample example = new YgDrawPrizeRecordExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        YgDrawPrizeRecord record = new YgDrawPrizeRecord();
        record.setStatus(DataStatus.Delete);

        return ygDrawPrizeRecordMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(YgDrawPrizeRecord ygDrawPrizeRecord) {
        int r = 0;
        if (ygDrawPrizeRecord.getId() != null) {
            r = this.update(ygDrawPrizeRecord);
        } else {
            r = this.save(ygDrawPrizeRecord);
        }
        return r;
    }

    @Override
    public int addRecord(PrizeItem prizeItem, YgUser user, String sourceCode) {
        YgDrawPrizeRecord ygDrawPrizeRecord = new YgDrawPrizeRecord(user.getId(), sourceCode, prizeItem.getType(), prizeItem.getRelationCode(),
                prizeItem.getThisRate(), prizeItem.getContent(), "", new Date());
        int i = this.save(ygDrawPrizeRecord);

        return i;
    }

    @Override
    public YgUserPrize findMyGoodsPrize(Long userId, String sourceCode) {
        return ygDrawPrizeRecordMapper.selectMyGoodsPrize(userId, sourceCode);
    }

    @Override
    public BigDecimal getGoodsPrizeCount(Long userId, String sourceCode) {
        return ygDrawPrizeRecordMapper.getGoodsPrizeCount(userId, sourceCode);
    }

    @Override
    public int addCashRecord(Long userId, Long cardId, BigDecimal count, String sourceCode) {
        YgDrawPrizeRecord ygDrawPrizeRecord = new YgDrawPrizeRecord(userId, sourceCode, Type.Goods, cardId.toString(), count.multiply(new BigDecimal("-1")),
                "兑换", "", new Date());
        return this.save(ygDrawPrizeRecord);

    }

    @Override
    public int countByUserId(Long userId, Type type, String sourceCode) {

        return ygDrawPrizeRecordMapper.countByUserId(userId, type, sourceCode);
    }

    @Override
    public BigDecimal getTotalRateByUserId(Long userId, Type type, String sourceCode) {
        BigDecimal sum = ygDrawPrizeRecordMapper.getTotalRateByUserId(userId, type, sourceCode);

        return sum == null ? new BigDecimal("0") : sum;
    }

    @Override
    public ResultSet<DrawReport> drawReport(String sourceCode) {
        List<DrawReport> scanList = ygDrawPrizeRecordMapper.getScanUserByDate(sourceCode);
        List<DrawReport> productList = ygDrawPrizeRecordMapper.getChangeProductByDate(sourceCode);
        for (DrawReport report : scanList) {
            for (DrawReport product : productList) {
                if (report.getCreateTime().equals(product.getCreateTime()))
                    report.setTotalProduct(product.getTotalProduct());
            }
        }
        ResultSet<DrawReport> resultSet = new ResultSet<DrawReport>(scanList.size(), scanList);
        return resultSet;

    }

    @Override
    public Map<String, Object> selectDrawSizeTotal(String sourceCode) {
        Map<String, Object> map = new HashMap<String, Object>();

        int totalProduct = ygDrawPrizeRecordMapper.totalProduct(sourceCode);
        int totalUser = ygDrawPrizeRecordMapper.totalUser(sourceCode);
        int totalHelp = ygDrawPrizeRecordMapper.totalHelp(sourceCode);
        int totalRegister = ygDrawPrizeRecordMapper.totalRegisterUser(sourceCode);

        map.put("totalProduct", totalProduct);
        map.put("totalUser", totalUser);
        map.put("totalHelp", totalHelp);
        map.put("totalRegister", totalRegister);
        return map;

    }

    @Override
    public List<YgDrawPrizeRecord> findByRelationCode(String sourceCode, String relationCode) {
        YgDrawPrizeRecordExample example = new YgDrawPrizeRecordExample();
        example.createCriteria().andSourceCodeEqualTo(sourceCode).andRelationCodeEqualTo(relationCode).andStatusEqualTo(DataStatus.Valid.name());
        return ygDrawPrizeRecordMapper.selectByExample(example);
    }

    @Override
    public int findCashCountByUserId(Long userId, String sourceCode, Date startDate, Date endDate, boolean lessRate) {
        YgDrawPrizeRecordExample example = new YgDrawPrizeRecordExample();
        Criteria criteria = example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        if (lessRate) {
            criteria.andRateLessThan(new BigDecimal("0"));
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (startDate != null) {
            criteria.andCreateTimeBetween(startDate, endDate);
        }
        if (StringUtils.isNotBlank(sourceCode)) {
            criteria.andSourceCodeEqualTo(sourceCode);
        }

        return ygDrawPrizeRecordMapper.countByExample(example);
    }

}
